package com.airsystem.pos.bengkel.controller.report;

import java.awt.Frame;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;

import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

import org.apache.log4j.Logger;

import com.airsystem.pos.bengkel.entity.DetailOrderLayanan;
import com.airsystem.pos.bengkel.entity.DetailPesanSparepart;
import com.airsystem.pos.bengkel.entity.Karyawan;
import com.airsystem.pos.bengkel.entity.Tagihan;
import com.airsystem.pos.bengkel.interfaces.IReport;
import com.airsystem.pos.bengkel.model.report.BalanceSheetReportModel;
import com.airsystem.pos.bengkel.service.LaporanBulananServices;
import com.airsystem.pos.bengkel.util.Preconditions;

/**
 * @author Budi Oktaviyan <budi.oktaviyan@icloud.com>
 */
public class BalanceSheetReportController extends BalanceSheetReportModel implements IReport {
	private static final Logger LOG = Logger.getLogger(BalanceSheetReportController.class.getSimpleName());

	private LaporanBulananServices laporanBulananServices = new LaporanBulananServices();

	public BalanceSheetReportController() {
		super();
	}

	public void print() {
		try {
			Preconditions.checkEmpty(bulanCombo);
			Preconditions.checkEmpty(tahunCombo);
		} catch (IllegalArgumentException e) {
			JOptionPane.showMessageDialog(this, "Silahkan pilih data!", "Pesan", JOptionPane.WARNING_MESSAGE);

			return;
		}

		try {
			/* Get all income data */
			List<BigDecimal> dataJasa = new ArrayList<BigDecimal>();
			for (DetailOrderLayanan detailOrderLayanan : laporanBulananServices.getAllJasa(getPeriode())) {
				if (detailOrderLayanan.getDetail_sparepart() != null) {
					dataJasa.add(detailOrderLayanan.getDetail_sparepart().getHarga_satuan()
							.multiply(new BigDecimal(detailOrderLayanan.getSparepartTerpakai()))
							.add(detailOrderLayanan.getDetail_layanan().getHarga()));
				} else {
					dataJasa.add(detailOrderLayanan.getDetail_layanan().getHarga()
							.add(new BigDecimal(0)));
				}
			}

			/* Get all expenses data */
			List<BigDecimal> dataKaryawan = new ArrayList<BigDecimal>();
			for (Karyawan karyawan : laporanBulananServices.print()) {
				dataKaryawan.add(karyawan.getGaji().add(karyawan.getUang_makan()));
			}

			List<Long> dataTagihan = new ArrayList<Long>();
			for (Tagihan tagihan : laporanBulananServices.print(getPeriode())) {
				dataTagihan.add(tagihan.getBiaya());
			}

			List<BigDecimal> dataSparepart = new ArrayList<BigDecimal>();
			for (DetailPesanSparepart detailPesanSparepart : laporanBulananServices.getAllSparepart(getPeriode())) {
				dataSparepart.add(BigDecimal.valueOf(detailPesanSparepart.getStok()).multiply(detailPesanSparepart.getHarga()));
			}

			List balanceComponents = new ArrayList();
			balanceComponents.add(dataJasa);
			balanceComponents.add(dataKaryawan);
			balanceComponents.add(dataTagihan);
			balanceComponents.add(dataSparepart);

			JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(balanceComponents);

			/* Calculate income data */
			BigDecimal totalJasa = new BigDecimal(0);
			for (BigDecimal result : dataJasa) {
				totalJasa = totalJasa.add(result);
			}

			/* Calculate expenses data */
			BigDecimal totalKaryawan = new BigDecimal(0);
			for (BigDecimal result : dataKaryawan) {
				totalKaryawan = totalKaryawan.add(result);
			}

			Long totalTagihan = new Long(0);
			for (Long result : dataTagihan) {
				totalTagihan += result;
			}

			BigDecimal totalSparepart = new BigDecimal(0);
			for (BigDecimal result : dataSparepart) {
				totalSparepart = totalSparepart.add(result);
			}

			Map<String, Object> parameter = new HashMap<String, Object>();
			parameter.put("periode_param", getPeriode().replace("/", " "));
			parameter.put("jasa", totalJasa);
			parameter.put("karyawan", totalKaryawan);
			parameter.put("tagihan", BigDecimal.valueOf(totalTagihan));
			parameter.put("sparepart", totalSparepart);

			InputStream inputStream = EmployeeReportController.class.getResourceAsStream("/reports/BalanceSheetReport.jasper");

			JasperReport jasperReport = (JasperReport) JRLoader.loadObject(inputStream);

			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameter, beanCollectionDataSource);

			jasperViewer = new JasperViewer(jasperPrint, false);
			jasperViewer.setTitle("Laporan Balance Sheet");
			jasperViewer.setExtendedState(Frame.MAXIMIZED_BOTH);
			jasperViewer.setVisible(true);
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(BalanceSheetReportController.this, "Load data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}
}